/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package BUS;

import DTO.ContactDTO;
import DTO.ContactGroupDTO;
import GUI.MyAddressFrame;
import GUI.MyInternalFrame;
import java.awt.Color;
import java.beans.PropertyVetoException;
import java.util.ArrayList;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTree;
import javax.swing.table.DefaultTableModel;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;

/**
 *
 * @author trquanglong
 */
public class ContactFunction {

    /** Show list of contacts on table */
      static public void showTableAllContacts(JTable tableContact, ArrayList<ContactDTO> lstContact){

          Vector rowHeader = new Vector ();
          rowHeader.add("Name");
          rowHeader.add("Email");
          rowHeader.add("Organization");
          rowHeader.add("Mobile");
          rowHeader.add("Home Phone");
          rowHeader.add("Work Phone");
          rowHeader.add("Fax");
          rowHeader.add("Home Address");
          rowHeader.add("Business Address");
          rowHeader.add("Country");
    

          DefaultTableModel model = new DefaultTableModel(rowHeader, 0);
          tableContact.setModel(model);

          for (int i = 0; i < lstContact.size(); i++){
              Vector rowData = new Vector();
              rowData.add(lstContact.get(i).getName());
              rowData.add(lstContact.get(i).getEmail());
              rowData.add(lstContact.get(i).getOrganization());
              rowData.add(lstContact.get(i).getMobile());
              rowData.add(lstContact.get(i).getHomePhone());
              rowData.add(lstContact.get(i).getWorkPhone());
              rowData.add(lstContact.get(i).getFax());
              rowData.add(lstContact.get(i).getHomeAddress());
              rowData.add(lstContact.get(i).getBusinessAddress());
              rowData.add(lstContact.get(i).getCountry());

              model.addRow(rowData);
          }
    }

      /**
       * Show list of contacts with Business Cards
       */
      static public void showContactBusiness (JPanel panelBusiness, ArrayList<ContactDTO> lstContact, int index){
          panelBusiness.removeAll();
          panelBusiness.repaint();
          panelBusiness.setLayout(new java.awt.GridLayout(0, 3, 20, 20));
          for (int i = 0; i < lstContact.size(); i++){
              MyInternalFrame myFrame = new MyInternalFrame(lstContact.get(i).getName(), lstContact.get(i).getOrganization(), lstContact.get(i).getMobile(),
                      lstContact.get(i).getHomePhone(), lstContact.get(i).getWorkPhone(), lstContact.get(i).getEmail(), lstContact.get(i).getAdditionalEmail());
              if (i == index)
                  myFrame.setBackground(Color.BLUE);
              panelBusiness.add(myFrame);
              myFrame.setVisible(true);
          }

      }


      /**
       * Show list of contacts with Business Cards
       */
      static public void showContactAddress (JPanel panelAddress, ArrayList<ContactDTO> lstContact, int index){
          panelAddress.removeAll();
          panelAddress.setLayout(new java.awt.GridLayout(0, 3, 20, 20));
          panelAddress.repaint();
          for (int i = 0; i < lstContact.size(); i++){
              MyAddressFrame myFrame = new MyAddressFrame(lstContact.get(i).getName(), lstContact.get(i).getEmail());
              panelAddress.add(myFrame);
              if (i == index)
                  myFrame.setBackground(Color.BLUE);
              myFrame.setVisible(true);
          }
      }


      /**
       * Show Tree Contacts with Groups
       */
      static public void showTreeContactGroups(JTree treeContact, ArrayList<ContactGroupDTO> lstContactGroup, ArrayList<ContactDTO> lstContact){
          DefaultMutableTreeNode root = (DefaultMutableTreeNode) treeContact.getModel().getRoot();
          root.removeAllChildren();
          for (int i = 0; i < lstContactGroup.size(); i++){
            DefaultMutableTreeNode nodeGroup = new DefaultMutableTreeNode(lstContactGroup.get(i).getNameGroup());
            for (int j = 0; j < lstContact.size(); j ++){
                 if (lstContact.get(j).getIdGroup() == lstContactGroup.get(i).getIdGroup()){
                     DefaultMutableTreeNode nodeContact = new DefaultMutableTreeNode(lstContact.get(j).getName());
                     nodeGroup.add(nodeContact);
                }
            }
            root.add(nodeGroup);
          }
          ((DefaultTreeModel)treeContact.getModel()).setRoot(root);
          treeContact.setSelectionRow(0);
      }

      /**
       * Show Tree Contacts All
       */
      static public void showTreeContactPhone (JTree treeContact, ArrayList<ContactDTO>lstContact){
          DefaultMutableTreeNode root = (DefaultMutableTreeNode) treeContact.getModel().getRoot();
          root.removeAllChildren();

          for (int i  = 0; i < lstContact.size(); i++){
                    DefaultMutableTreeNode nodeContact = new DefaultMutableTreeNode(lstContact.get(i).getName());
                    root.add(nodeContact);
          }
          ((DefaultTreeModel)treeContact.getModel()).setRoot(root);
          treeContact.setSelectionRow(0);
      }

      /**
       * Show Tree Contacts Company
       *
       */
      static public void showTreeContactCompany(JTree treeContact, ArrayList<String> lstCompany, ArrayList<ContactDTO> lstContact){
          DefaultMutableTreeNode root = (DefaultMutableTreeNode) treeContact.getModel().getRoot();
          root.removeAllChildren();
          for (int i = 0; i < lstCompany.size(); i++){
            DefaultMutableTreeNode nodeGroup = new DefaultMutableTreeNode(lstCompany.get(i));
            for (int j = 0; j < lstContact.size(); j ++){
                 if (lstContact.get(j).getOrganization().equals(lstCompany.get(i))){
                     DefaultMutableTreeNode nodeContact = new DefaultMutableTreeNode(lstContact.get(j).getName());
                     nodeGroup.add(nodeContact);
                }
            }
            root.add(nodeGroup);
          }
          ((DefaultTreeModel)treeContact.getModel()).setRoot(root);
          treeContact.setSelectionRow(0);
      }

      /**
       * Show tree Contacts Location
       */
      static public void showTreeContactLocation(JTree treeContact, ArrayList<String> lstLocation, ArrayList <ContactDTO> lstContact){
           DefaultMutableTreeNode root = (DefaultMutableTreeNode) treeContact.getModel().getRoot();
          root.removeAllChildren();
          for (int i = 0; i < lstLocation.size(); i++){
            DefaultMutableTreeNode nodeGroup = new DefaultMutableTreeNode(lstLocation.get(i));
            for (int j = 0; j < lstContact.size(); j ++){
                 if (lstContact.get(j).getCountry().equals(lstLocation.get(i))){
                     DefaultMutableTreeNode nodeContact = new DefaultMutableTreeNode(lstContact.get(j).getName());
                     nodeGroup.add(nodeContact);
                }
            }
            root.add(nodeGroup);
          }
          ((DefaultTreeModel)treeContact.getModel()).setRoot(root);
          treeContact.setSelectionRow(0);
      }

      /**
       * Show tree Contacts Data File
       */
      static public void showTreeContactData (JTree treeContact, ArrayList<String> lstData, ArrayList<ContactDTO> lstContact){
          DefaultMutableTreeNode root = (DefaultMutableTreeNode) treeContact.getModel().getRoot();
          root.removeAllChildren();
          DefaultMutableTreeNode nodeGroup = new DefaultMutableTreeNode("");


            for (int j = 0; j < lstContact.size(); j ++){
                   DefaultMutableTreeNode nodeContact = new DefaultMutableTreeNode(lstContact.get(j).getName());
                   nodeGroup.add(nodeContact);
            }
          root.add(nodeGroup);
          ((DefaultTreeModel)treeContact.getModel()).setRoot(root);
          treeContact.setSelectionRow(0);
      }

      /**
       * Show tree Contacts Bussiness Cards
       */


}
